package com.demo.web;

import com.demo.common.Constants;
import com.demo.common.LadingBillConstants;
import com.demo.dto.BaseResult;
import com.demo.dto.ListResult;
import com.demo.dto.Pagination;
import com.demo.entity.*;
import com.demo.service.*;
import com.demo.entity.*;
import com.demo.service.*;
import com.demo.vo.LadingBillVo;
import com.demo.vo.UserVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("/admin/ladingBill")
public class LadingBillController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ILadingBillService service;
    @Autowired
    private IOrderService orderervice;
    @Autowired
    private ILadingOrderService ladingOrderervice;
    @Autowired
    private IOfferService offerService;
    @Autowired
    private IStoreService storeService;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private ILadingBillLogService lblService;


    /**
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public BaseResult<Object> get(LadingBill arg, HttpSession session) {
        LadingBill service_res = service.selectByPrimaryKey(arg.getId());
        Map map = new HashMap();
        map.put("ladingBill", service_res);
        List<LadingOrder> lo = ladingOrderervice.selectByLadingId(arg.getId().toString());
        map.put("ladingBillDetail", lo);
        BaseResult res = new BaseResult(true, map);
        return res;
    }

    @ResponseBody
    @RequestMapping(value = "/createUnPaied", method = RequestMethod.POST)
    public BaseResult<Object> createUnPaied(LadingBillVo arg, HttpSession session) {
        UserVo user = (UserVo) session.getAttribute(Constants.USER);
        arg.setUserId(String.valueOf(user.getId()));
//        arg.setNo(Constants.generateServiceNO("T"));//提单号4
        arg.setApplierType("0");
        arg.setCheckStatus("1");
        arg.setChecker(user.getUserName());
        arg.setCheckTime(new Date());
        Object service_res = service.create(arg, false);


//        orderervice.selectByPrimaryKey();
        Boolean isManger = false;
        for (Role role : user.getRoles()) {
            if (String.valueOf(role.getId()).equals(Constants.SUPER_MANAGER_ROLE_ID)) { /// 超级管理员账户判断
                isManger = true;
            }
        }
        if (service_res instanceof String) {
            return new BaseResult(false, service_res.toString(), 403);
        }
        BaseResult res = new BaseResult(true, service_res);
        return res;
    }

    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public BaseResult<Object> create(LadingBillVo arg, HttpSession session) {
        UserVo user = (UserVo) session.getAttribute(Constants.USER);
        arg.setUserId(String.valueOf(user.getId()));
        arg.setChecker(user.getUserName());
        arg.setCheckTime(new Date());
        arg.setApplierType("0");
        arg.setCheckStatus("1");
        Object service_res = service.create(arg, false);

        Boolean isManger = false;
        for (Role role : user.getRoles()) {
            if (String.valueOf(role.getId()).equals(Constants.SUPER_MANAGER_ROLE_ID)) { /// 超级管理员账户判断
                isManger = true;
            }
        }

        if (service_res instanceof String) {
            return new BaseResult(false, service_res.toString(), 403);
        }
        BaseResult res = new BaseResult(true, service_res);
        return res;
    }

    /**
     * 打印提单
     *
     * @param arg
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/print", method = RequestMethod.GET)
    public BaseResult<Object> print(LadingBill arg, HttpSession session) {

        User user = (User) session.getAttribute(Constants.USER);

        if (user == null) {
            return new BaseResult(false, "未登录!", 401);
        }
        LadingBill lb = service.selectByPrimaryKey(arg.getId());
        if (lb == null) {
            return new BaseResult(false, "参数错误!", 403);
        }
        Order order = orderervice.selectByPrimaryKey(Integer.valueOf(lb.getOrderId()));
        Store store = storeService.selectByPrimaryKey(Integer.valueOf(lb.getStoreId()));
        Offer offer = offerService.selectByPrimaryKey(Integer.valueOf(order.getOfferId()));
        Map map = new HashMap();
        map.put("storeCom", store.getName());
        map.put("storeName", store.getName());
        map.put("idCard", lb.getCarId());
        map.put("qualitySpec", offer.getQualitySpec());
        map.put("goodsName", offer.getGoodsName());
        map.put("number", lb.getNumberThisTime());
        map.put("userName", user.getName());
        map.put("no", lb.getNo());
        if ("0".equals(arg.getType())) {
            map.put("expireDate", lb.getExpireTime());
        } else if ("2".equals(arg.getType())) {
            map.put("delegate", lb.getCarId());
            map.put("freeDate", lb.getCarId());
        } else if ("3".equals(arg.getType())) {

        }
        Map lb_list = new HashMap();
        lb_list.put("ladingBill", lb);
        lb_list.put("store", lb);
        lb_list.put("print", map);
        BaseResult<Object> res = new BaseResult<Object>(true, lb_list);
        return res;
    }

    /**
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public BaseResult<Integer> delete(LadingBill arg) {

        int service_res = service.deleteByPrimaryKey(arg.getId());
        BaseResult<Integer> res = new BaseResult<Integer>(true, service_res);

        return res;
    }

    /**
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResult<Integer> update(LadingBill arg) {
        int service_res = service.updateByPrimaryKeySelective(arg);
        BaseResult<Integer> res = new BaseResult<Integer>(true, service_res);
        return res;
    }

    @ResponseBody
    @RequestMapping(value = "/update_labing_bill_actual", method = RequestMethod.POST)
    public BaseResult<Object> update_labing_bill_actual(LadingOrder arg, HttpSession session) {

        User user = (User) session.getAttribute(Constants.USER);

        if (user == null) {
            return new BaseResult(false, "未登录!", 401);
        }

//        arg.setActual(Arith.add(arg.getActual(), se_res.getActual()));
        Object obj = service.update_labing_bill_actual(arg);
        if (obj instanceof Integer) {
            int obj_ = Integer.valueOf(obj.toString());
            LadingOrder se_res = ladingOrderervice.selectByPrimaryKey(arg.getId());
            lblService.insertLog(String.valueOf(user.getId()), arg.getId().toString(), LadingBillConstants.OPERATE_ADD_ACTUAL_NUMBER, arg.getActual().toString());
            return new BaseResult(true, obj_);
        } else {
            return new BaseResult(false, obj.toString(), 403);
        }
    }

    /***
     * 分页查询
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ListResult query(LadingBillVo arg, HttpSession session) {

        UserVo user = (UserVo) session.getAttribute(Constants.USER);
        if (user == null) {
            return new ListResult(false, "未登录!", 401);
        }
        Boolean isManger = false;
        for (Role role : user.getRoles()) {
            if (String.valueOf(role.getId()).equals(Constants.SUPER_MANAGER_ROLE_ID)) { /// 超级管理员账户判断
                isManger = true;
            }
        }
        if (!isManger) { //运营账户判断判断
            arg.setCarId(String.valueOf(user.getId()));
        }

        Goods g = new Goods();
        g.setOperatorId(String.valueOf(user.getId()));
        if (!isManger && (goodsService.isOperator(g) == 0)) {
            return new ListResult(false, "非运营或超级管理员角色账号，无权访问!", 403);
        }
        List<LadingBill> list = service.queryByPage(arg);
        int total = service.queryCount(arg);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        ListResult res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }
}
